#!/usr/bin/perl
# to select the overlap portion of 1_flow_table.csv, in particular, when sending rate of flow1 > flow

#folders
$root_folder="/root/switch_project";
$result_folder="$root_folder/experiment/exp23"; #root dir to store experimental resutls
$aggregated_result="$result_folder/linear_model_stat.csv"; #results for all experiments, used for plot

$runs=1; # number of repeated experiment (same setting)
$extra_num_packets=50; # number of extra packets to include (ensure all overlap is included)

@speed;

#specify sending rate
$start_sending_rate=100;#Mb/s
$end_sending_rate=1000;
$sending_rate_gap=100;

#calculate speed and gap (gap is not used for now)
for($b=$start_sending_rate, $a=0; $b<=$end_sending_rate; $b+=$sending_rate_gap, $a++)
{
	$speed[$a]=$b;
}

open(TOTAL_OUT, ">$aggregated_result") or die "$aggregated_result cannot be opened.\n";
for($j=0; $j<=$#speed; $j++)
#for($j=8; $j<=8; $j++)
{
	for($k=0; $k<=$#speed; $k++)
	#for($k=0; $k<=0; $k++)
	{
		for($i=1; $i<=$runs; $i++)
		{	
			chdir "$result_folder/$speed[$j]M/$speed[$k]M/$i";
			print "folder = $result_folder/$speed[$j]M/$speed[$k]M/$i\n";

			if($speed[$j] >= $speed[$k])
			{
				$fast_flow="1_flow_table.csv";
			}
			else
			{
				$fast_flow="2_flow_table.csv";
			}

			$last_line=`tail -n 1 $result_folder/$speed[$j]M/$speed[$k]M/$i/$fast_flow`;
			print("$last_line\n");
			@last_line_block=split(', ', $last_line);
			$end_time = $last_line_block[4];
			print("end_time = $end_time\n");	
			
			#extract <arrival time, delay> of every last packet before loss and first packet after loss
			print "Extracting packet before/after loss... $result_folder/$speed[$j]M/$speed[$k]M/$i/ \n";
			$in="$result_folder/$speed[$j]M/$speed[$k]M/$i/flow_table_sorted.csv";
			$out="$result_folder/$speed[$j]M/$speed[$k]M/$i/flow_pkt_before_after_loss.csv";
			open(IN, $in) or die "$in cannot be opened.\n";
			open(OUT, ">$out") or die "$out cannot be opened.\n";

			$prev_delay=-1;
			$prev_arrival_timestamp=-1;

			$line=<IN>;				
			chomp($line);
			@line_block=split(', ', $line);				
			$prev_arrival_timestamp=$line_block[0];
			$prev_delay=$line_block[4];
			print OUT "$prev_arrival_timestamp, 13.2\n";
				
			while(<IN>)
			{
				my($line)=$_;
				chomp($line);
				@line_block=split(', ', $line);

				if($line_block[5]>=$end_time)
				{
					last;
				}
				
				$cur_arrival_timestamp=$line_block[0];
				$cur_delay=$line_block[4];
				#print "cur_arrival_timestamp = $cur_arrival_timestamp, cur_delay = $cur_delay\n";
				#print "prev_arrival_timestamp = $prev_arrival_timestamp, prev_delay = $prev_delay\n";
				
				if($cur_delay==0 && $prev_delay>0) #found last packet before loss
				{
					print OUT "$prev_arrival_timestamp, $prev_delay\n";
				}
				elsif($prev_delay==0 && $cur_delay>0) #found first packet after loss
				{
					print OUT "$cur_arrival_timestamp, $cur_delay\n";
				}		
				$prev_delay = $cur_delay;
				$prev_arrival_timestamp = $cur_arrival_timestamp;
			}

			close(IN);
			close(OUT);			
	
			#compute first increasing rate, and first loss period and output to aggregate result
			print "Computing increasing_rate and loss_period... $result_folder/$speed[$j]M/$speed[$k]M/$i/ \n";
			$in2="$result_folder/$speed[$j]M/$speed[$k]M/$i/flow_pkt_before_after_loss.csv";
			open(IN2, $in2) or die "$in2 cannot be opened.\n";
	
			$line1 = <IN2>;
			$line2 = <IN2>;
			$line3 = <IN2>;
			if($line1=='' || $line2=='' ||$line3=='')
			{
				print TOTAL_OUT "$speed[$j], $speed[$k], -1, -1\n";
				last;
			}

			chomp($line1);
			@line1_block=split(', ', $line1);
			chomp($line2);
			@line2_block=split(', ', $line2);
			chomp($line3);
			@line3_block=split(', ', $line3);

			$timestamp=$line_block[0];
			$prev_delay=$line_block[1];

			$increase_rate=($line2_block[1]-$line1_block[1])/($line2_block[0]-$line1_block[0]);
			$loss_period=$line3_block[0]-$line2_block[0];

			print TOTAL_OUT "$speed[$j], $speed[$k], $increase_rate, $loss_period\n";

		}
	}
}

close(IN2);
close(TOTAL_OUT);

chdir "$root_folder/script";

sub compute_k_l_avg
{

			#compute increasing_rate and loss_episode
				print "Computing increasing_rate and loss_episode... $result_folder/$speed[$j]M/$speed[$k]M/$i/ \n";
				#$in="$result_folder/$speed[$j]M/$speed[$k]M/$i/" . $w . "_flow_pkt_before_after_loss.csv";
				#$out="$result_folder/$speed[$j]M/$speed[$k]M/$i/" . $w . "_flow_k_l.csv";
				$in="$result_folder/$speed[$j]M/$speed[$k]M/$i/flow_pkt_before_after_loss.csv";
				$out="$result_folder/$speed[$j]M/$speed[$k]M/$i/flow_k_l.csv";
				open(IN, $in) or die "$in cannot be opened.\n";
				open(OUT, ">$out") or die "$out cannot be opened.\n";
				open(TOTAL_OUT, ">$aggregated_result") or die "$aggregated_result cannot be opened.\n";

				$line = <IN>;
				chomp($line);
				@line_block=split(', ', $line);
				$prev_timestamp=$line_block[0];
				$prev_delay=$line_block[1];

				$counter=1;
				$counter_k=0;
				$counter_l=0;
				$sum_k=0;
				$sum_l=0;

				while(<IN>)
				{
					my($line)=$_;
					chomp($line);
					@line_block=split(', ', $line);
					$timestamp=$line_block[0];
					$delay=$line_block[1];

					if($counter % 2 ==1) #compute increase_rate
					{
						$ir=($delay-$prev_delay)/($timestamp-$prev_timestamp);
						print OUT "k, $ir\n";
						#$sum_k=$sum_k+$ir;
						#$counter_k=$counter_k+1;
						
					}
					else #compute le
					{
						$le=$timestamp-$prev_timestamp;
						print OUT "l, $le\n";
						#$sum_l=$sum_l+$le;
						#$counter_l=$counter_l+1;
					}

					$prev_timestamp=$timestamp;
					$prev_delay=$delay;
					$counter=$counter+1;
				}
				
				#$avg_k = $sum_k/$counter_k;
				#$avg_l = $sum_l/$counter_l;
				#print TOTAL_OUT "$speed[$j], $speed[$k], $avg_k, $avg_l\n";

				close(IN);
				close(OUT);				
				close(TOTAL_OUT);
}
